package org.ktpro.shortlink.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.micrometer.core.annotation.Timed;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.ktpro.shortlink.project.common.convention.result.Result;
import org.ktpro.shortlink.project.common.convention.result.Results;
import org.ktpro.shortlink.project.dto.req.ShortLinkGroupStatsReqDTO;
import org.ktpro.shortlink.project.dto.req.ShortLinkStatsAccessRecordGroupReqDTO;
import org.ktpro.shortlink.project.dto.req.ShortLinkStatsAccessRecordReqDTO;
import org.ktpro.shortlink.project.dto.req.ShortLinkStatsReqDTO;
import org.ktpro.shortlink.project.dto.resp.ShortLinkStatsAccessRecordRespDTO;
import org.ktpro.shortlink.project.dto.resp.ShortLinkStatsRespDTO;
import org.ktpro.shortlink.project.service.ShortLinkStatsService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 短链接监控控制层
 */
@RestController
@Slf4j
@RequiredArgsConstructor
public class ShortLinkStatsController {

    private final ShortLinkStatsService shortLinkStatsService;

    /**
     * 访问单个短链接指定时间内监控数据
     */
    @Timed(value = "stats-singleLink", description = "查看单个短链接监控")
    @GetMapping("/api/short-link/v1/stats")
    public Result<ShortLinkStatsRespDTO> shortLinkStats(ShortLinkStatsReqDTO requestParam) {
        return Results.success(shortLinkStatsService.oneShortLinkStats(requestParam));
    }

    /**
     * 指定时间内短链接访问记录监控
     */
    @Timed(value = "stats-singleLinkRecord", description = "查看单个短链接访问记录")
    @GetMapping("/api/short-link/v1/stats/access-record")
    public Result<IPage<ShortLinkStatsAccessRecordRespDTO>> shortLinkAccessRecordStats(ShortLinkStatsAccessRecordReqDTO requestParam) {
        return Results.success(shortLinkStatsService.pageLinkStatsAccessRecord(requestParam));
    }

    /**
     * 指定时间内分组内所有短链接的访问记录统计
     */
    @Timed(value = "stats-GroupLink", description = "查看分组短链接监控数据")
    @GetMapping("/api/short-link/v1/stats/group")
    public Result<ShortLinkStatsRespDTO> shortLinkStatsGroup(ShortLinkGroupStatsReqDTO requestParam) {
        return Results.success(shortLinkStatsService.groupShortLinkStats(requestParam));
    }

    /**
     * 指定时间内分组访问记录监控
     */
    @Timed(value = "stats-GroupLinkRecord", description = "查看分组短链接访问记录")
    @GetMapping("/api/short-link/v1/stats/access-record/group")
    public Result<IPage<ShortLinkStatsAccessRecordRespDTO>> shortLinkAccessRecordStatsByGroup(ShortLinkStatsAccessRecordGroupReqDTO requestParam) {
        return Results.success(shortLinkStatsService.pageLinkStatsAccessRecordByGroup(requestParam));
    }

}
